package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.service.gov.mapper.OfficeTaskKsConfigMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeTaskKsConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskOffLineConfig;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskOnLineConfig;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTaskKsConfigService;

import cn.hutool.core.collection.CollUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 配置表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-11-24
 */
@Service
public class OfficeTaskKsConfigServiceImpl extends BaseServiceImpl<OfficeTaskKsConfigMapper, OfficeTaskKsConfig> implements IOfficeTaskKsConfigService {

    @Override
    public void initOfficeKsConfig(String officeId) {
        Integer count = lambdaQuery()
                .eq(OfficeTaskKsConfig::getOfficeId, officeId)
                .eq(OfficeTaskKsConfig::getDelFlag, false)
                .count();
        if (count <= 0) {
            baseMapper.initOfficeKsConfig(officeId);
        }
    }

    @Override
    public List<TaskOnLineConfig> getTaskOnLineConfigByOfficeId(String officeId) {

        List<OfficeTaskKsConfig> officeTaskKsConfigs = this.lambdaQuery()
                .eq(OfficeTaskKsConfig::getOfficeId, officeId)
                .eq(OfficeTaskKsConfig::getDelFlag, false)
                .list();

        if (CollUtil.isEmpty(officeTaskKsConfigs)) {
            //初始化
            initOfficeKsConfig(officeId);
            //查询
            officeTaskKsConfigs = this.lambdaQuery()
                    .eq(OfficeTaskKsConfig::getOfficeId, officeId)
                    .eq(OfficeTaskKsConfig::getDelFlag, false)
                    .list();
        }

        List<TaskOnLineConfig> taskOnLineConfigs = new ArrayList<>();
        officeTaskKsConfigs.stream().forEach(officeTaskKsConfig -> {
            TaskOnLineConfig taskOnLineConfig = new TaskOnLineConfig();
            BeanUtils.copyProperties(officeTaskKsConfig, taskOnLineConfig);
            taskOnLineConfigs.add(taskOnLineConfig);
        });
        
        // 需要过滤掉相同configCode的数据
        Map<String, TaskOnLineConfig> coonfigCodeMap = new HashMap<>();
        for (Iterator<TaskOnLineConfig> iterator = taskOnLineConfigs.iterator(); iterator.hasNext();) {
            TaskOnLineConfig pushExamConfigParamTemp =  iterator.next();
            if(coonfigCodeMap.containsKey(pushExamConfigParamTemp.getConfigCode())) {
                iterator.remove();
            }
            coonfigCodeMap.put(pushExamConfigParamTemp.getConfigCode(), pushExamConfigParamTemp);
        }

        return taskOnLineConfigs;
    }

    @Override
    public List<OfficeTaskKsConfig> getOfficeTaskKsConfig_sys() {
        List<OfficeTaskKsConfig> officeTaskKsConfigs = this.lambdaQuery()
                .eq(OfficeTaskKsConfig::getLevel, 1)
                .eq(OfficeTaskKsConfig::getDelFlag, false)
                .list();
        return officeTaskKsConfigs;
    }

}
